/**
 * 这个文件才是真正前端出发数据传递要调用的api
 * 角色表相关
 * 增删改相关
 */

import { ref } from "vue"
import { type AddRoleModel } from '@/api/role/RoleModel'
import { EditType } from '@/type/BaseEnum'
import { type FuncList } from '@/type/BaseType'
import { deleteApi } from '@/api/role'
import { ElMessage } from 'element-plus'
import useInstance from '@/hooks/useInstance'
import type {Result} from "@/http";
export default function useRole(getList: FuncList) {
    const { global } = useInstance()
    //新增弹框的ref属性(子组件将方法暴露出去，父组件就可以调用子组件的方法)
    const addRef = ref<{ show: (type: string, row?: AddRoleModel)
            => void }>()

    //新增
    const addBtn = () => {
        addRef.value?.show(EditType.ADD)
    }
    //编辑
    const editBtn = (row: AddRoleModel) => {
        addRef.value?.show(EditType.EDIT, row);
    }
    //删除
    const deleteBtn = async (row: AddRoleModel) => {
        let confirm = await global.$myconfirm('确定删除该数据吗?')
        if (confirm) {
            let res:Result = await deleteApi(row.roleId)
            if (res && res.code == 200) {
                ElMessage.success(res.msg)
                getList()
            }
        }
    }
    return {
        addBtn,
        editBtn,
        deleteBtn,
        addRef
}
}